/*
 * RemoteTableDemo.java
 *
 * Written by Joseph Bowbeer and released to the public domain,
 * as explained at http://creativecommons.org/licenses/publicdomain
 */

package jozart.remotetabledemo;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableModel;

import jozart.remotetable.RemoteTableModel;
import jozart.remotetable.RemoteTableModelAdapter;

/**
 * Runs a remote editor, remote viewer, and remote table model
 * together in one applet.
 *
 * @author  Joseph Bowbeer
 * @version 1.4
 */
public class RemoteTableDemo extends JApplet {

    private JPanel jPanel1;
    private JPanel jPanel2;

    private RemoteTableEditor editor;
    private RemoteTableViewer viewer;

    /** Initializes the form. */
    public RemoteTableDemo() {
        initComponents();

        try {

            /* Create a demo remote table model locally. */
            TableModel model = DemoTableModel.createServerModel();
            final RemoteTableModel adapter = new RemoteTableModelAdapter(model);

            /* Create an editor linked to the remote model. */
            editor = new RemoteTableEditor() {
                protected RemoteTableModel lookupRemoteModel() {
                    return adapter;
                }
            };

            /* Create a viewer linked to the remote model. */
            viewer = new RemoteTableViewer() {
                protected RemoteTableModel lookupRemoteModel() {
                    return adapter;
                }
            };

            jPanel1.add(editor);
            jPanel2.add(viewer);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Called from within the constructor to initialize the form.
     */
    private void initComponents() {
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        getContentPane().setLayout(new GridLayout(2, 1));

        jPanel1.setBorder(new CompoundBorder(
            new EmptyBorder(new Insets(10, 4, 4, 4)),
            new TitledBorder(new EtchedBorder(), "Editor", 1, 2,
                new Font("Dialog", 1, 14))));

        getContentPane().add(jPanel1);

        jPanel2.setBorder(new CompoundBorder(
            new EmptyBorder(new Insets(10, 4, 4, 4)),
            new TitledBorder(new EtchedBorder(), "Viewer", 1, 2,
                new Font("Dialog", 1, 14))));

        getContentPane().add(jPanel2);
    }

    /** Starts the editor and viewer. */
    public void start() {
        editor.start();
        viewer.start();
    }

    /** Stops the editor and viewer. */
    public void stop() {
        editor.stop();
        viewer.stop();
    }

    /**
     * Asks the editor and viewer to shutdown and waits up to
     * <tt>timeout</tt> milliseconds for each to complete.
     * Called from <tt>main</tt> method just before calling
     * <tt>exit</tt>.
     * 
     * @param timeout milliseconds to wait for editor and viewer shutdown
     */
    public void shutdown(int timeout) {
        editor.shutdown(timeout);
        viewer.shutdown(timeout);
    }


    /**
     * Launches this applet in a frame.
     * @param args ignored
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame("Remote Table Demo");
        final RemoteTableDemo applet = new RemoteTableDemo();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                applet.shutdown(1000);
                System.exit(0);
            }
        });
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.pack();
                frame.setVisible(true);
                applet.init();
                applet.start();
            }
        });
    }
}
